package com.lenovo.smarttraffic.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager;
import com.lenovo.smarttraffic.ui.adapter.BasePagerAdapter;
import com.lenovo.smarttraffic.ui.fragment.FirstFragment;
import com.lenovo.smarttraffic.ui.fragment.FragmentFactory;
import com.lenovo.smarttraffic.ui.fragment.SecondFragment;
import com.lenovo.smarttraffic.ui.fragment.ThirdFragment;
import com.lenovo.smarttraffic.ui.fragment.fgBean.DragReturnBean;
import com.lenovo.smarttraffic.util.CommonUtil;
import com.lenovo.smarttraffic.zm.item1.AddItemActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Amoly
 * @date 2019/4/11.
 * description： 交通资讯页面
 */

public class Item1Activity extends BaseActivity {
    @BindView(R.id.tab_layout_list)
    TabLayout tabLayoutList;
    @BindView(R.id.header_layout)
    LinearLayout headerLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private ImageView iv_add;
    private BasePagerAdapter basePagerAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitView();
        InitData();
        EventBus.getDefault().register(Item1Activity.this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_list_tab;
    }

    private void InitView() {
        initToolBar(findViewById(R.id.toolbar), true, getString(R.string.item1));
        //viewpager 中 adapter getPageTitle  实现联动
        tabLayoutList.setupWithViewPager(viewPager);
        tabLayoutList.setTabMode(TabLayout.MODE_SCROLLABLE);
        headerLayout.setBackgroundColor(CommonUtil.getInstance().getColor());
        iv_add = findViewById(R.id.iv_add);
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Item1Activity.this, AddItemActivity.class);
                startActivity(intent);
            }
        });

    }

    private void InitData() {
        basePagerAdapter = new BasePagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(basePagerAdapter);
        viewPager.setOffscreenPageLimit(2);//viewpager是默认预加载下一页的界面的。
        // viewpager提供了一个设置预加载页面数量的方法
        //  比正常多加载1个fragment
    }

    //界面排序
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(DragReturnBean bean) {
        basePagerAdapter = new BasePagerAdapter(getSupportFragmentManager(),bean.getListstr());
        viewPager.setAdapter(basePagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        basePagerAdapter.notifyDataSetChanged();
        Toast.makeText(Item1Activity.this, "MessageEventBus  ", Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.e("============", "onResume: " + "Item1Activity");
        headerLayout.setBackgroundColor(CommonUtil.getInstance().getColor());
        //不好使
        //  basePagerAdapter.recreateItems(,);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("============", "onDestroy: " + "Item1Activity");
        EventBus.getDefault().unregister(Item1Activity.this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("============", "onPause: " + "Item1Activity");
    }
}
